//
//  ShareViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 13-2-22.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "ShareViewController.h"
#import "UIImageView+WebCache.h"
#import "SinaWeiboManager.h"
#import <QuartzCore/QuartzCore.h>

@interface ShareViewController ()

@property (nonatomic,retain) NSString *addiditon;

@end

@implementation ShareViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    titleLabel.text = [_info valueForKey:@"item_name"];
    tipsLabel.text = [NSString stringWithFormat:@"//分享#中邮听书#：《%@》",[_info valueForKey:@"item_name"]];
    NSString *imageURL = [[_info valueForKey:@"image"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [coverImageView setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"default_image"] options:SDWebImageRetryFailed];
    self.addiditon = [NSString stringWithFormat:@"//分享#中邮听书#：《%@》@中邮阅读",[_info valueForKey:@"item_name"]];
    
    coverImageView.layer.shadowColor = [UIColor blackColor].CGColor;
    coverImageView.layer.shadowOpacity = 0.8f;
    coverImageView.layer.shadowOffset = CGSizeMake(-0.0f, -0.0f);
    coverImageView.layer.masksToBounds = NO;
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:coverImageView.bounds];
    coverImageView.layer.shadowPath = path.CGPath;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [commentTextView becomeFirstResponder];
}

- (IBAction)commitShare:(id)sender {
    [commentTextView resignFirstResponder];
    [self showLoadingView];
    switch (_shareType) {
        case ShareTypeSinaWeibo:
        {
            NSString *comment = commentTextView.text;
            NSString *finalComment = [NSString stringWithFormat:@"%@%@",comment,_addiditon];
            [[[SinaWeiboManager defaultManager] sinaweibo] requestWithURL:@"statuses/upload.json" params:[NSMutableDictionary dictionaryWithObjectsAndKeys:finalComment, @"status",coverImageView.image, @"pic", nil] httpMethod:@"POST" delegate:self];
            
            [[DataRequest sharedRequest] commentBookWithID:[_info valueForKey:@"item_id"] comment:finalComment rate:10];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - SinaWeiboRequest Delegate

- (void)request:(SinaWeiboRequest *)request didFailWithError:(NSError *)error
{
    [self removeloadingView];
    if ([request.url hasSuffix:@"statuses/upload.json"])
    {
        ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"发送失败"] autorelease];
        [promptView show];
        
        NSLog(@"分享失败 : %@", error);
    }
}

- (void)request:(SinaWeiboRequest *)request didFinishLoadingWithResult:(id)result
{
    [self removeloadingView];
    if ([request.url hasSuffix:@"statuses/upload.json"])
    {
        NSLog(@"分享成功");
        ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"发送成功"] autorelease];
        [promptView show];
        [self navBack];
    }
}

- (IBAction)navBack
{
    UINavigationController *navController = (UINavigationController *)self.view.window.rootViewController;
    [navController popViewControllerAnimated:YES];
}

- (void)showLoadingView
{
    if (loadingView.superview) {
        return;
    }
    if (loadingView == nil) {
        loadingView = [[[ABSpringLoadingView alloc] initWithFrame:contentView.frame] autorelease];
    }
    loadingView.alpha = 0;
    [self.view addSubview:loadingView];
    [UIView animateWithDuration:0.3 animations:^{
        loadingView.alpha = 1;
    }];
}

- (void)removeloadingView
{
    if (loadingView) {
        [UIView animateWithDuration:0.3 animations:^{
            loadingView.alpha = 0;
        } completion:^(BOOL finished) {
            [loadingView removeFromSuperview];
            loadingView = nil;
        }];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [titleLabel release];
    [commentTextView release];
    [coverImageView release];
    [_info release];
    [tipsLabel release];
    [contentView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [titleLabel release];
    titleLabel = nil;
    [commentTextView release];
    commentTextView = nil;
    [coverImageView release];
    coverImageView = nil;
    [tipsLabel release];
    tipsLabel = nil;
    [contentView release];
    contentView = nil;
    [super viewDidUnload];
}
@end
